package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

@Service(interfaceClass= OrderService.class)
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderMapper orderMapper;


    /**
     * 1.检查用户所选择的预约日期是否已经提供了预约设置，如果没有设置则无法进行预约
     * 2.检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
     * 3.通过手机号检查当前用户是否为会员
     *       不是会员：自动完成注册
     *       是会员：查询是否重复预约
     * 4.添加预约
     * 5.增加预约人数
     *
     * @param map
     * @return
     */
    @Override
    public Order add(Map map) throws Exception {
        //1.检查用户所选择的预约日期是否已经提供了预约设置，如果没有设置则无法进行预约
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingMapper.findCountByDate(date);
        if (orderSetting == null) {
            throw new RuntimeException(MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //2.检查是否存在已经满预约的服务日期
        if (orderSetting.getReservations() >= orderSetting.getNumber()) {
            throw new RuntimeException(MessageConstant.ORDER_FULL);
        }

        //3.通过手机号检查当前用户是否为会员
        String telephone = (String) map.get("telephone");
        Member member = memberMapper.checkMemberIsNot(telephone);

        if (member == null) {
            //如果不是会员，注册为会员(注意回显ID)
            member = new Member();
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setRegTime(new Date());
            member.setSex((String) map.get("sex"));
            member.setName((String) map.get("name"));
            //执行注册
            memberMapper.add(member);
        } else {
            //如果是会员，则根据 预约时间，会员ID，套餐ID 三者 来判断是否重复
            Order order = new Order();
            order.setId(member.getId());
            order.setOrderDate(date);
            order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));

            Integer count = orderMapper.findCountByOrder(order);
            if (count > 0) {
                throw new RuntimeException(MessageConstant.HAS_ORDERED);
            }

        }
        //4.添加预约
        Order newOrder = new Order();
        newOrder.setMemberId(member.getId());
        newOrder.setOrderDate(date);
        newOrder.setOrderType((String)map.get("orderType"));
        newOrder.setOrderStatus(Order.ORDERSTATUS_NO);
        newOrder.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
        orderMapper.add(newOrder);

        //5.根据日期预约人数+1
        orderSettingMapper.addOrderSettingByorderDate(date);

        return newOrder;
    }

    /**
     * 根据预约ID获取体检人名字member
     * 根据预约ID获取体检套餐名字setmeal
     * 根据预约ID获取体检日期orderDate
     * 根据预约ID获取预约类型orderType
     * @param id
     * @return
     */
    @Override
    public Map findById(Integer id) throws Exception {

        Map map = orderMapper.findById(id);

        //转换日期格式，否则页面上的日期会有时分秒
        Date orderDate = (Date) map.get("orderDate");
        map.put("orderDate",DateUtils.parseDate2String(orderDate));

        return map;
    }

    /**
     * 检查该号码是否属于现有会员
     * @param telephone
     * @return 返回true为现有会员 返回false不是现有会员
     */
    @Override
    public Member check(String telephone) {
        return memberMapper.checkMemberIsNot(telephone);
    }
}
